package cn.agiledata.bank.information.service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import cn.agiledata.bank.common.dao.SysTemplate;
import cn.agiledata.bank.common.dao.TransactionTemplate;
import cn.agiledata.bank.common.service.email.TransactionEmail;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.DateUtil;
import cn.agiledata.bank.common.util.UniqueIdentityUtil;
import cn.agiledata.bank.hibernateBean.MobileChargeRecord;
import cn.agiledata.bank.hibernateBean.SysParameter;
import cn.agiledata.bank.hibernateBean.Transaction;
import cn.agiledata.bank.information.dao.MobileChargeDao;

import com.vnv.infrastructure.email.service.IMailService;
import com.vnv.infrastructure.email.service.VNVMail;
import com.vnv.infrastructure.logic.IPBAppFactory;

/**
 * 手机充值Service
 * 
 * @author 马贤波
 *
 */
public class MobileChargeServiceImpl implements MobileChargeService {
	
	private MobileChargeDao mobileChargeDao;
	
	private TransactionTemplate transactionTemplate;
	private IMailService notificationService;// 邮件发送服务
	
	public IMailService getNotificationService() {
		return notificationService;
	}

	public void setNotificationService(IMailService notificationService) {
		this.notificationService = notificationService;
	}


	public MobileChargeDao getMobileChargeDao() {
		return mobileChargeDao;
	}

	public void setMobileChargeDao(MobileChargeDao mobileChargeDao) {
		this.mobileChargeDao = mobileChargeDao;
	}

	public TransactionTemplate getTransactionTemplate() {
		return transactionTemplate;
	}

	public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
		this.transactionTemplate = transactionTemplate;
	}

	/** 
	 * 判断当天是否可以再次充值
	 */
	public boolean isLegalCharge(Long accountId, int tradTime) {
		int times = mobileChargeDao.getCurrentPayTime(accountId);
		if (times < tradTime) 
			return true;
		else
			return false;
	}
	
	public MobileChargeRecord createRecord(String mobileNum, String chargeType, String email, long accountId) {
		// 创建充值记录
		MobileChargeRecord record = new MobileChargeRecord();
		record.setMobileNum(mobileNum);
		record.setChargeType(new Integer(chargeType));
		record.setEmail(email);
		record.setCreateTime(new Date());
		record.setAccountSeq(new Long(accountId));
		
		// 取得充值实际金额
		record.setRealAmount(getChargeValue(chargeType, accountId));
		record.setStatus(Constant.MOBILE_CHARGE_STATUS_CREATE);
		
		// 取得记录订单号
		String strRecordNo = UniqueIdentityUtil.getMobileChargeRecordNo();
		record.setRecordNo(strRecordNo);
		
		mobileChargeDao.saveOrUpdateRecored(record);

		return record;
	}
	
	public void sendMail(MobileChargeRecord record) {

		VNVMail mail = new VNVMail();
		Map vnvMap = new HashMap();
		mail.setEmailTo(record.getEmail());
		mail.setSubject("您的手机充值通知");
		vnvMap.put("mobile", record.getMobileNum());
		vnvMap.put("sendTime", DateUtil.date2String(new Date()));
		vnvMap.put("recordNo", record.getTransaction().getTransId());
		
		String strParamCharge = null;
		if(record.getChargeType().intValue() == Constant.MOBILE_CHARGE_TYPE_50) {
			// 充值50元
			strParamCharge = "50";
		}
		if(record.getChargeType().intValue() == Constant.MOBILE_CHARGE_TYPE_100) {
			// 充值100元
			strParamCharge = "100";
		}
		vnvMap.put("amount", strParamCharge);
		vnvMap.put("realAmount", record.getRealAmount());
		mail.setContext(vnvMap);
		mail.setTemplateName("mail-mobileCharge.vm");
		notificationService.sendMail(mail);
		
		TransactionEmail tranMail = (TransactionEmail) IPBAppFactory.getObj("transactionMail");
		String transId = record.getTransaction().getTransId();
		Transaction trans = transactionTemplate.getTransactionByTranId(transId);
		String strTransTrail = "";
		if (!(transId == null || transId.length() < 6)) {
			strTransTrail = transId.substring(transId.length() - 6);
        }
		Map mapParam = new HashMap();
		mapParam.put("time", DateUtil.date2String(new Date(),"M月d日"));
		mapParam.put("chargAmt", strParamCharge);
		mapParam.put("amount", trans.getAmount());
		mapParam.put("shortTranId", strTransTrail);
		tranMail.sendSmsByParam(record.getMobileNum(),"sms-mobile_charge.vm",mapParam);
	}
	
	/**
	 * 取得当前实际充值手机扣除金额
	 * 
	 * @param strChargeType
	 * @return
	 */
	private Double getChargeValue(String strChargeType, long accountId) {
		SysTemplate sysTemplate = (SysTemplate) IPBAppFactory
				.getObj("sysTemplate");
		String strParamCharge = null;
		if(Integer.parseInt(strChargeType) == Constant.MOBILE_CHARGE_TYPE_50) {
			// 充值50元
			strParamCharge = Constant.SYS_PARAM_MOBILE_CHARGE_50;
			if (accountId != 0)strParamCharge = Constant.SYS_PARAM_MOBILE_CHARGE_UU_50;
		}
		if(Integer.parseInt(strChargeType) == Constant.MOBILE_CHARGE_TYPE_100) {
			// 充值100元
			strParamCharge = Constant.SYS_PARAM_MOBILE_CHARGE_100;
			if (accountId != 0)strParamCharge = Constant.SYS_PARAM_MOBILE_CHARGE_UU_100;
		}
		SysParameter param = sysTemplate.getParamByType(strParamCharge);
		String strRealAmt = param.getParmValue();
		
		return new Double(strRealAmt);
	}

	public MobileChargeRecord getRecordByNo(String recordNo) {
		// 根据订单号取得充值记录
		return mobileChargeDao.getRecordByNo(recordNo);
	}

	public synchronized MobileChargeRecord recordPayConfirm(String recordNo, String transId) {
		// 支付成功，修改充值记录状态
		// 取得充值记录
		MobileChargeRecord record = getRecordByNo(recordNo);
		
		if(Constant.MOBILE_CHARGE_STATUS_SUBMIT.equals(record.getStatus())) {
			// 多次通知，状态已经改变，不再进行通知
			return record;
		}
		
		// 取得相关交易
		Transaction trans = transactionTemplate.getTransactionByTranId(transId);
		
		// 修改记录状态为已提交（未充值）
		record.setTransaction(trans);
		record.setStatus(Constant.MOBILE_CHARGE_STATUS_SUBMIT);
		
		record = mobileChargeDao.saveOrUpdateRecored(record);
		
		// 进行邮件或者短信通知
		MobileChargeService mcService = (MobileChargeService) IPBAppFactory
				.getObj("mobileChargeService");
		mcService.sendMail(record);
		
		return record;
	}

}
